﻿using Neudesic.Pulse.Entities;
using System.ComponentModel.Composition;

namespace Neudesic.Pulse.Services
{
    /// <summary>
    /// Adapter that uses the Pulse SDK to provide services to the viewmodel.
    /// </summary>
    [Export(typeof(IPulseGateway))]
    [PartCreationPolicy(CreationPolicy.Shared)]
    public class PulseGatewayAdapter : IPulseGateway
    {

        PulseClient client;

        /// <summary>
        /// Initializes a new instance of the <see cref="PulseGatewayAdapter" /> class.
        /// </summary>
        /// <param name="configurationService">The configuration service.</param>
        public PulseGatewayAdapter()
        {
            client = new PulseClient(ConfigurationService.ServiceBaseAddress, AuthenticationScheme.Ntlm);
        }

        /// <summary>
        /// Creates the status.
        /// </summary>
        /// <param name="status">The status.</param>
        public void CreateStatus(string status)
        {
            client.CreateStatus(new StatusRequest() { Text = status }, client.CurrentUser);
        }

        /// <summary>
        /// Gets the activities.
        /// </summary>
        /// <remarks>
        /// Returns the latest list of activities if <paramref name="newer"/> is left empty.  Otherwise, the activities since the last retrieval.
        /// </remarks>
        /// <param name="newer">The newer.</param>
        /// <returns></returns>
        public PagedActivityList GetActivities(string newer = "")
        {
            if (!string.IsNullOrEmpty(newer))
                return client.GetPagedActivityList(newer); // latest since last request

            else
                return client.GetPagedActivityList(client.CurrentUser); // default feed
        }

        /// <summary>
        /// Gets the entity that represents the currently running user.
        /// </summary>
        /// <returns></returns>
        public Entity GetMe()
        {
            return client.CurrentUser;
        }
    }
}
